﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Reflection;
using System.IO;
namespace WoodproService
{
    public partial class WPService : ServiceBase
    {

        private string mstrRecalcQtyFileName = "C:\\Program Files\\WoodPro Software Inc\\WoodPro InSight" + "\\ImRecalcQty.XmL";
        private string mstrImDailyInterestToCommCostFileName = "C:\\Program Files\\WoodPro Software Inc\\WoodPro InSight" + "\\ImDailyInterestToCommCost.XmL";
        private System.Timers.Timer timer1 = new System.Timers.Timer();
   
        public WPService()
        {
            InitializeComponent();

        }

        protected override void OnStart(string[] args)
        {

            this.timer1.Enabled = true;
            this.timer1.Interval = 5000;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick); 
            this.timer1.Start();
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。






            this.timer1.Stop(); 
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            RecalItemQty();
            //DebugXML("startIntrest0.XML", "doing");
            RecalcInterst();
            //DebugXML("startIntrest9.XML", "doing");
            //if need ,can add other procedure in following code.

        }

        private void RecalcInterst()
        {
          //  DebugXML("startIntrest1.XML", "doing");
            DataTable dtDailyInterestToCommCost = new DataTable();
            string strFileName = "";
           
            try
            {
                strFileName = mstrImDailyInterestToCommCostFileName;
                //DebugXML("readint1.XML", "doing");
                if (!File.Exists(strFileName))
                {
                    strFileName = mstrImDailyInterestToCommCostFileName.ToUpper().Replace("PROGRAM FILES", "PROGRAM FILES (X86)");
                    //DebugXML("readint2.XML", "doing");
                    if (!File.Exists(strFileName))
                    {
                        strFileName = mstrImDailyInterestToCommCostFileName.ToUpper().Replace("C:", "D:");
                        //DebugXML("readint3.XML", "doing");
                        if (!File.Exists(strFileName))
                        {
                            strFileName = mstrImDailyInterestToCommCostFileName.ToUpper().Replace("C:", "D:").Replace("PROGRAM FILES", "PROGRAM FILES (X86)");
                            //DebugXML("readint4.XML", "doing");
                            if (!File.Exists(strFileName)) return;
                        }
                    }

                }
                //DebugXML("readint5.XML", "doing");

                dtDailyInterestToCommCost.ReadXml(strFileName);
                if (dtDailyInterestToCommCost.Rows.Count == 0)
                {
                    return;
                }

                DataRow dr = dtDailyInterestToCommCost.Rows[0];
             //   DebugXML("startIntrest2.XML", "doing");
                string strConDB = CvtFunc.ToString(dr["ConnectString"]);
                string strRunTime = CvtFunc.ToString(dr["StartTime"]).TrimEnd();
                if (!(DateTime.Parse(strRunTime).Hour == DateTime.Now.Hour && DateTime.Parse(strRunTime).Minute == DateTime.Now.Minute)) return;
                Type[] ptype = null;
                Type typeRecal = null;
                ptype = new Type[1];
                ptype.SetValue(typeof(string), 0);


                typeRecal = Type.GetType("WP.IM.DataAccess.ImDailyChrgInterestToCommCostDAL," + "WP.IM.DataAccess", true, true);
                if (typeRecal != null)
                {
                    object oRpt = Activator.CreateInstance(typeRecal);
                    MethodInfo mInfo = oRpt.GetType().GetMethod("DailyInterestToCommssionCostFromOverNight", ptype);
                    string[] strServerandDB = strConDB.Split(new string[] { "\r\n" } , StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < strServerandDB.Length; i++)
                    {
                        object[] Param = { strServerandDB[i] };
                        if (mInfo != null)
                        {

                            this.timer1.Stop();
                            System.Threading.Thread.Sleep(70000);
                            this.timer1.Start();
                           
                            object obj = mInfo.Invoke(oRpt, Param);
                            obj = null;


                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // DebugXML("ErrorRecalcInterst" + ".Xml", ex.Message);
            }
        }
        private void RecalItemQty()
        {

            DataTable dtRecalItemQty = new DataTable();
            //DebugXML("DirTest" + ".Xml", Environment.CurrentDirectory);
            //DebugXML("startItemQty1.XML", "doing");
            try
            {
                string strFileName = mstrRecalcQtyFileName;
                //DebugXML("readint1.XML", "doing");
                if (!File.Exists(strFileName))
                {
                    strFileName = mstrRecalcQtyFileName.ToUpper().Replace("PROGRAM FILES", "PROGRAM FILES (X86)");
                    //DebugXML("readint2.XML", "doing");
                    if (!File.Exists(strFileName))
                    {
                        strFileName = mstrRecalcQtyFileName.ToUpper().Replace("C:", "D:");
                        //DebugXML("readint3.XML", "doing");
                        if (!File.Exists(strFileName))
                        {
                            strFileName = mstrRecalcQtyFileName.ToUpper().Replace("C:", "D:").Replace("PROGRAM FILES", "PROGRAM FILES (X86)");
                            //DebugXML("readint4.XML", "doing");
                            if (!File.Exists(strFileName)) return;
                        }
                    }

                }

                dtRecalItemQty.ReadXml(strFileName);
              

                DataRow dr = dtRecalItemQty.Rows[0];
                bool mblnNeedCalcBackOrder = CvtFunc.ToBoolean(dr["CalcBackOrder"]);
                bool mblnNeedCalcCommitted = CvtFunc.ToBoolean(dr["CalcCommitted"]);
                bool mblnNeedCalcOeReturn = CvtFunc.ToBoolean(dr["CalcReturn"]);
                bool mblnNeedCalcOnHand = CvtFunc.ToBoolean(dr["CalcOnhand"]);
                bool mblnNeedCalcOnOrder = CvtFunc.ToBoolean(dr["CalcOnOrder"]);
                bool mblnNeedCalcReman = CvtFunc.ToBoolean(dr["CalcReman"]);
                bool mblnNeedCalcTagLot = CvtFunc.ToBoolean(dr["CalcTagLot"]);
                bool mblnNeedCalcTrasfer = CvtFunc.ToBoolean(dr["CalcTransfer"]);
                bool mblnNeedCalcImAdjQty = CvtFunc.ToBoolean(dr["CalcAdjust"]);
                bool mblnNeedCalcQuoteRemaining = CvtFunc.ToBoolean(dr["CalcQuoteRemain"]);
                string strConDB = CvtFunc.ToString(dr["ConnectString"]);
                string strRunTime = CvtFunc.ToString(dr["StartTime"]).TrimEnd();
                if (!(DateTime.Parse(strRunTime).Hour == DateTime.Now.Hour && DateTime.Parse(strRunTime).Minute == DateTime.Now.Minute)) return;
                Type[] ptype = null;
                Type typeRecal = null;
                ptype = new Type[11];
                ptype.SetValue(typeof(string), 0);
                ptype.SetValue(typeof(bool), 1);
                ptype.SetValue(typeof(bool), 2);
                ptype.SetValue(typeof(bool), 3);
                ptype.SetValue(typeof(bool), 4);
                ptype.SetValue(typeof(bool), 5);
                ptype.SetValue(typeof(bool), 6);
                ptype.SetValue(typeof(bool), 7);
                ptype.SetValue(typeof(bool), 8);
                ptype.SetValue(typeof(bool), 9);
                ptype.SetValue(typeof(bool), 10);

                typeRecal = Type.GetType("WP.IM.DataAccess.ImRecalculateItemQtyDAL," + "WP.IM.DataAccess", true, true);
                if (typeRecal != null)
                {
                    object oRpt = Activator.CreateInstance(typeRecal);
                    MethodInfo mInfo = oRpt.GetType().GetMethod("RecalcQtyFromOvernight", ptype);
                    string[] strServerandDB = strConDB.Split(new string[] { "\r\n" } , StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < strServerandDB.Length; i++)
                    {
                        object[] Param = {strServerandDB[i] , mblnNeedCalcBackOrder, mblnNeedCalcOnOrder, mblnNeedCalcReman,
                                           mblnNeedCalcTrasfer, mblnNeedCalcCommitted, mblnNeedCalcOnHand,
                                           mblnNeedCalcTagLot, mblnNeedCalcOeReturn, mblnNeedCalcImAdjQty, mblnNeedCalcQuoteRemaining };
                        if (mInfo != null)
                        {
                            this.timer1.Stop();
                            System.Threading.Thread.Sleep(70000);
                            this.timer1.Start();

                            object obj = mInfo.Invoke(oRpt, Param);
                            obj = null;
                            
                        }
                    }
                }
            }
            catch (Exception ex)
            {
              //  DebugXML("ErrorTest" + ".Xml", ex.Message);

            }
          
        }

        private void DebugXML(string strFileName, string strErr)
        {
            #region Recalcualte item Qty
            string strDebugFileName = "d:\\" + strFileName;
            DataTable dtRecalItemQty = new DataTable("ImRecalcQty");
            dtRecalItemQty.Columns.Add(new DataColumn("ConnectString", typeof(string)));
            dtRecalItemQty.Columns.Add(new DataColumn("StartTime", typeof(string)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcBackOrder", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcOnhand", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcCommitted", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcReman", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcTransfer", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcOnOrder", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcTagLot", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcReturn", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcAdjust", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcEffectWtAvgcost", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcQuoteRemain", typeof(bool)));
            dtRecalItemQty.Rows.Add();
            DataRow dr = dtRecalItemQty.Rows[0];
            dr["ConnectString"] = strErr;
            dr["StartTime"] = "23:00";
            dr["CalcBackOrder"] = true;
            dr["CalcOnhand"] = true;
            dr["CalcCommitted"] = true;
            dr["CalcReman"] = true;
            dr["CalcTransfer"] = true;
            dr["CalcOnOrder"] = true;
            dr["CalcTagLot"] = true;
            dr["CalcReturn"] = true;
            dr["CalcAdjust"] = true;
            dr["CalcEffectWtAvgcost"] = true;
            dr["CalcQuoteRemain"] = true;
            dtRecalItemQty.WriteXml(strDebugFileName, XmlWriteMode.WriteSchema, false);
            #endregion

        }


        private void MakeXML(string strFileName)
        {
            #region Recalcualte item Qty 
            DataTable dtRecalItemQty = new DataTable("ImRecalcQty");
            dtRecalItemQty.Columns.Add(new DataColumn("ConnectString", typeof(string)));
            dtRecalItemQty.Columns.Add(new DataColumn("StartTime", typeof(string)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcBackOrder", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcOnhand", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcCommitted", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcReman", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcTransfer", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcOnOrder", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcTagLot", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcReturn", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcAdjust", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcEffectWtAvgcost", typeof(bool)));
            dtRecalItemQty.Columns.Add(new DataColumn("CalcQuoteRemain", typeof(bool)));
            dtRecalItemQty.Rows.Add();
            DataRow dr = dtRecalItemQty.Rows[0];
            dr["ConnectString"] = "";
            dr["StartTime"] = "23:00";
            dr["CalcBackOrder"] = true;
            dr["CalcOnhand"] = true;
            dr["CalcCommitted"] = true;
            dr["CalcReman"] = true;
            dr["CalcTransfer"] = true;
            dr["CalcOnOrder"] = true;
            dr["CalcTagLot"] = true;
            dr["CalcReturn"] = true;
            dr["CalcAdjust"] = true;
            dr["CalcEffectWtAvgcost"] = true;
            dr["CalcQuoteRemain"] = true;
            dtRecalItemQty.WriteXml(mstrRecalcQtyFileName, XmlWriteMode.WriteSchema, false);
            #endregion

            #region recalcualte interest .
            #endregion
        }
    }
}
